package inventory.controler;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultListModel;
import javax.swing.JTable;
import javax.swing.ListModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

import utilites.Constants;
import utilites.ViewUtilites;
import dto.Gin;
import dto.MaterialMaster;
import dto.MaterialSpecification;
import dto.Stock;
import inventory.daoImplementer.GinImplementor;
import inventory.daoImplementer.GinItemsImplementor;
import inventory.daoImplementer.MaterialMasterImplementor;
import inventory.daoImplementer.MaterialSpecificationImplementor;
import inventory.daoImplementer.StockInImplementor;
import inventory.daoInterface.GinDao;
import inventory.daoInterface.GinItemsDao;
import inventory.daoInterface.MaterialMasterDao;
import inventory.daoInterface.MaterialSpecificationDao;
import inventory.daoInterface.StockInDao;
import inventory.model.GinItems;
import inventory.model.Material;
import inventory.view.Inward;

public class GinControler implements ActionListener,ListSelectionListener 
{
	private Gin model;
	private Inward view;
	private GinItems[]ginItems=null;
	
	public GinControler() 
	{
	
	}
	
	public GinControler(Gin model, Inward view) 
	{
		super();
		this.model = model;
		this.view = view;
		view.addActionListener(this);
		
		setListGin();
	}
	
	//****************************************************************************************************

	private void setListGin()
	{
		GinDao imp=new GinImplementor();
		Gin[] ginarray = null;
		try {
			ginarray = imp.retreiveArray("status", Constants.ACTIVE+"");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		String[] ginNo=new String[ginarray.length];
		
		for(int i=0;i<ginNo.length;i++)
			ginNo[i]=ginarray[i].getGinNo()+","+ginarray[i].getChDate();
		
		ViewUtilites.setList(view.getList(),ginNo);
	}
	
	private void setGinTable(String selection)
	{
		GinItemsDao items=new GinItemsImplementor();
		
		try {
			ginItems=items.retreiveArray("ginNo",selection);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		String[] data=new String[ginItems.length];
		
		for(int i=0;i<data.length;i++)
		{
			data[i]=ginItems[i].getmCode();	
		}
		
		for(int k=0;k<data.length;k++)
		{
			MaterialMasterDao imp=new MaterialMasterImplementor();
			MaterialMaster mastere = null;
			try {
				mastere = imp.retreive("mCode",data[k]);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			MaterialSpecificationDao impSpec=new MaterialSpecificationImplementor();
			MaterialSpecification specs = null;
			try {
				specs = impSpec.retreive("mCode", data[k]);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			Material material=new Material(mastere,specs);
			ginItems[k].setMaterial(material);
		}
		
		
		JTable temp=view.getTable();
		int sr=0;
		DefaultTableModel dm=(DefaultTableModel) temp.getModel();
		for(GinItems item:ginItems)
		{
			dm.addRow(new Object[]{sr,item.getMaterial().getMname(),item.getMaterial().getSpecs(),item.getMaterial().getUnit(),item.getmReceivedQuant()});
			sr++;
		}	
		
	}

	public void reactOnSave()
	{
		for(GinItems temp :ginItems)
		{
			Stock si=new Stock();
			si.setGinNo(temp.getGinNumber());
			si.setmCode(temp.getmCode());
			si.setMquant(temp.getmReceivedQuant());
			si.setStockCode(1+"");
			
			StockInDao sid=new StockInImplementor();
			
			try {
				sid.add(si);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			String listItem=view.getList().getSelectedValue().toString();
			DefaultListModel lm=(DefaultListModel) view.getList().getModel();
			
			lm.removeElement(listItem);
			
			ViewUtilites.refreshTable(view.getTable());
		}
	}
	
	//**********************************************************************************************
	@Override
	public void valueChanged(ListSelectionEvent arg0) 
	{
		 if (arg0.getValueIsAdjusting());
			 
		 else
		 {
			 String selection= view.getList().getSelectedValue().toString();
			 
			 selection=selection.split(",")[0];
			 
			 setGinTable(selection);
		 }
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) 
	{
		
		
	}
}
